package com.sth.decorator;

/**
 * 装饰器模式: 在不链接其他其结构的情况下,向现有对象添加新功能
 * 一种结构型模式,因为他充当现有类的装饰器
 * 创建一个装饰器类来包装原始类并提供其他功能
 */
public class AppMain {
    public static void main(String[] args) {
        Printer plasticPrinter = new PlasticPrinter();
        Printer printer3D = new Printer3D(new PlasticPrinter());
        Printer3D printer3D1 = new Printer3D(new PaperPrinter());
        plasticPrinter.print();
        printer3D.print();
        printer3D1.print();
    }
}

interface Printer {
    void print();
}

class PaperPrinter implements Printer {
    @Override
    public void print() {
        System.out.println("打印纸张-----");
    }
}

class PlasticPrinter implements Printer {
    @Override
    public void print() {
        System.out.println("打印塑料纸+++++");
    }
}

abstract class PrinterDecorator implements Printer {
    protected Printer decoratorPrinter;

    public PrinterDecorator(Printer d) {
        this.decoratorPrinter = d;
    }

    public void print() {
        decoratorPrinter.print();
    }
}

class Printer3D extends PrinterDecorator {
    public Printer3D(Printer d) {
        super(d);
    }
    @Override
    public void print() {
        System.out.println("3D----增强了功能");
        decoratorPrinter.print();
    }
}


